﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using FlexCell;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YJKRevitBase.WinForm;

namespace YJKFamilyComparison
{
	public partial class FrmCompareResult : System.Windows.Forms.Form
	{
		internal FrmCompareResult(DocumentResultPacker documentresult1, DocumentResultPacker documentresult2)
		{
			this.DocumentResult1 = documentresult1;
			this.DocumentResult2 = documentresult2;
			this.InitializeComponent();
		}

		internal FrmCompareResult(DocumentResultPacker documentresult1, DocumentResultPacker documentresult2, WindowHandle revitHandle)
		{
			this.m_RevitHandle = revitHandle;
			this.DocumentResult1 = documentresult1;
			this.DocumentResult2 = documentresult2;
			this.InitializeComponent();
		}

		private void FrmCompareResult_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			List<DocumentResultPacker> list = new List<DocumentResultPacker>();
			List<DocumentResultPacker> list2 = new List<DocumentResultPacker>();
			ResultPackerBase resultPackerBase = null;
			ResultPackerBase resultPackerBase2 = null;
			List<DocumentResultPacker> lstLinkDocumentResults = this.DocumentResult1.LstLinkDocumentResults;
			List<DocumentResultPacker> lstLinkDocumentResults2 = this.DocumentResult2.LstLinkDocumentResults;
			this.OrderLinkDocuments(ref lstLinkDocumentResults, ref lstLinkDocumentResults2);
			for (int i = 0; i < lstLinkDocumentResults.Count; i++)
			{
				resultPackerBase = lstLinkDocumentResults[i];
				resultPackerBase2 = lstLinkDocumentResults2[i];
				Utility.CompareAndFillNode_DFS(ref resultPackerBase, resultPackerBase2);
				Utility.CompareAndFillNode_DFS(ref resultPackerBase2, resultPackerBase);
				resultPackerBase.OrderChildPacker();
				resultPackerBase2.OrderChildPacker();
				resultPackerBase.ChildPackers.Order();
				resultPackerBase2.ChildPackers.Order();
				list.Add(resultPackerBase as DocumentResultPacker);
				list2.Add(resultPackerBase2 as DocumentResultPacker);
			}
			this.DocumentResult1.LstLinkDocumentResults = list;
			this.DocumentResult2.LstLinkDocumentResults = list2;
			ResultPackerBase documentResult = this.DocumentResult1;
			ResultPackerBase documentResult2 = this.DocumentResult2;
			documentResult.OrderChildPacker();
			documentResult2.OrderChildPacker();
			Utility.CompareAndFillNode_DFS(ref documentResult, documentResult2);
			Utility.CompareAndFillNode_DFS(ref documentResult2, documentResult);
			documentResult.ChildPackers.Order();
			documentResult2.ChildPackers.Order();
			this.TreeViewInit(documentResult as DocumentResultPacker, ref this.tvFileResultList1, documentResult2 as DocumentResultPacker);
			this.TreeViewInit(documentResult2 as DocumentResultPacker, ref this.tvFileResultList2, documentResult as DocumentResultPacker);
		}

		private void OrderLinkDocuments(ref List<DocumentResultPacker> tempLstLinkDocumentResults1, ref List<DocumentResultPacker> tempLstLinkDocumentResults2)
		{
			tempLstLinkDocumentResults1 = (from k in tempLstLinkDocumentResults1
			orderby k.Name.Replace(k.Name.Substring(0, k.Name.LastIndexOf("\\")), "")
			select k).ToList<DocumentResultPacker>();
			tempLstLinkDocumentResults2 = (from k in tempLstLinkDocumentResults2
			orderby k.Name.Replace(k.Name.Substring(0, k.Name.LastIndexOf("\\")), "")
			select k).ToList<DocumentResultPacker>();
		}

		private void TreeViewInit(DocumentResultPacker documentresultpacker1, ref TreeView tv, DocumentResultPacker documentresultpacker2)
		{
			TreeNode treeNode = new TreeNode(documentresultpacker1.NdName);
			treeNode.Name = "主文档";
			this.GetNode(documentresultpacker1, ref treeNode, documentresultpacker2);
			tv.Nodes.Add(treeNode);
			if (documentresultpacker1.LstLinkDocumentResults.Count > 0)
			{
				for (int i = 0; i < documentresultpacker1.LstLinkDocumentResults.Count; i++)
				{
					TreeNode treeNode2 = new TreeNode(documentresultpacker1.LstLinkDocumentResults[i].NdName);
					treeNode2.Name = "链接文档";
					this.GetNode(documentresultpacker1.LstLinkDocumentResults[i], ref treeNode2, documentresultpacker2.LstLinkDocumentResults[i]);
					tv.Nodes.Add(treeNode2);
				}
			}
			tv.ExpandAll();
		}

		private bool IfElementParametersEqual(ElementTypeResultPacker elementtype1, ElementTypeResultPacker elementtype2)
		{
			if (elementtype1 == null || elementtype2 == null)
			{
				return false;
			}
			Dictionary<string, List<KeyValuePair<string, string>>> parameters = elementtype1.parameters;
			Dictionary<string, List<KeyValuePair<string, string>>> parameters2 = elementtype2.parameters;
			if (elementtype1.parameters.Count != elementtype2.parameters.Count)
			{
				return false;
			}
			for (int i = 0; i < parameters.Count; i++)
			{
				for (int j = 0; j < parameters.ElementAt(i).Value.Count; j++)
				{
					KeyValuePair<string, string> keyValuePair = parameters.ElementAt(i).Value[j];
					KeyValuePair<string, string> keyValuePair2 = parameters2.ElementAt(i).Value[j];
					if (keyValuePair.Value != keyValuePair2.Value)
					{
						return false;
					}
				}
			}
			return true;
		}

		private void GetNode(ResultPackerBase resultpacker1, ref TreeNode treeNode, ResultPackerBase resultpacker2)
		{
			if (resultpacker1.ChildPackers.CountSum > 0)
			{
				for (int i = 0; i < resultpacker1.ChildPackers.ThisValue.Count; i++)
				{
					for (int j = 0; j < resultpacker1.ChildPackers.ThisValue.ElementAt(i).Value.Count; j++)
					{
						ResultPackerBase resultPackerBase = resultpacker1.ChildPackers.ThisValue.ElementAt(i).Value[j];
						ResultPackerBase resultPackerBase2 = resultpacker2.ChildPackers.ThisValue.ElementAt(i).Value[j];
						try
						{
							TreeNode treeNode2 = new TreeNode(resultPackerBase.NdName);
							ElementTypeResultPacker elementTypeResultPacker = resultPackerBase as ElementTypeResultPacker;
							ElementTypeResultPacker elementTypeResultPacker2 = resultPackerBase2 as ElementTypeResultPacker;
							if (elementTypeResultPacker != null || elementTypeResultPacker2 != null)
							{
								treeNode2.Tag = resultPackerBase;
								treeNode2.Name = resultPackerBase.Name;
								if (resultPackerBase.NdName != resultPackerBase2.NdName || !this.IfElementParametersEqual(elementTypeResultPacker, elementTypeResultPacker2))
								{
									treeNode2.ForeColor = System.Drawing.Color.Red;
								}
								else
								{
									treeNode2.ForeColor = System.Drawing.Color.Black;
								}
								if (!(System.Drawing.Color.Black == treeNode2.ForeColor))
								{
									if (resultPackerBase.NdName.Equals("无") || resultPackerBase2.NdName.Equals("无"))
									{
										treeNode2.ForeColor = System.Drawing.Color.Black;
									}
									treeNode2.Tag = resultPackerBase;
									treeNode.Nodes.Add(treeNode2);
								}
							}
							else
							{
								this.GetNode(resultPackerBase, ref treeNode2, resultPackerBase2);
								if (treeNode2.Nodes.Count > 0)
								{
									treeNode2.Tag = resultPackerBase;
									treeNode.Nodes.Add(treeNode2);
								}
							}
						}
						catch (Exception)
						{
							TreeNode treeNode3 = new TreeNode(resultPackerBase.NdName);
							treeNode3.Tag = resultPackerBase;
							treeNode3.Name = resultPackerBase.Name;
							treeNode.Nodes.Add(treeNode3);
							this.GetNode(resultPackerBase, ref treeNode3, resultPackerBase2);
						}
					}
				}
			}
		}

		private void FindSameNode(TreeNode inputNode, TreeNode conditionNode, ref TreeNode outNode)
		{
			if (inputNode.Nodes.Count != 0)
			{
				for (int i = 0; i < inputNode.Nodes.Count; i++)
				{
					TreeNode treeNode = inputNode.Nodes[i];
					ResultPackerBase obj = conditionNode.Tag as ResultPackerBase;
					if ((treeNode.Tag as ResultPackerBase).Equals(obj))
					{
						outNode = treeNode;
					}
					this.FindSameNode(treeNode, conditionNode, ref outNode);
				}
			}
		}

		private void tvFileResultList1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
		{
			if (e.Node.Nodes.Count == 0 && e.Node.Parent != null)
			{
				try
				{
					FrmCompareResult.tnTreeView1PreSelectedNode.BackColor = System.Drawing.Color.White;
					FrmCompareResult.tnTreeView2PreSelectedNode.BackColor = System.Drawing.Color.White;
				}
				catch
				{
				}
				TreeNode selectedNode = null;
				for (int i = 0; i < this.tvFileResultList2.Nodes.Count; i++)
				{
					this.FindSameNode(this.tvFileResultList2.Nodes[i], e.Node, ref selectedNode);
				}
				e.Node.BackColor = System.Drawing.Color.Yellow;
				FrmCompareResult.tnTreeView1PreSelectedNode = e.Node;
				this.tvFileResultList2.SelectedNode = selectedNode;
				this.tvFileResultList2.SelectedNode.BackColor = System.Drawing.Color.Yellow;
				FrmCompareResult.tnTreeView2PreSelectedNode = this.tvFileResultList2.SelectedNode;
			}
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void tvFileResultList2_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
		{
			if (e.Node.Nodes.Count == 0 && e.Node.Parent != null)
			{
				int index = e.Node.Parent.Parent.Parent.Index;
				try
				{
					FrmCompareResult.tnTreeView1PreSelectedNode.BackColor = System.Drawing.Color.White;
					FrmCompareResult.tnTreeView2PreSelectedNode.BackColor = System.Drawing.Color.White;
				}
				catch
				{
				}
				this.tvFileResultList1.AutoScrollOffset = this.tvFileResultList2.AutoScrollOffset;
				TreeNode selectedNode = null;
				this.FindSameNode(this.tvFileResultList1.Nodes[index], e.Node, ref selectedNode);
				e.Node.BackColor = System.Drawing.Color.Yellow;
				FrmCompareResult.tnTreeView2PreSelectedNode = e.Node;
				this.tvFileResultList1.SelectedNode = selectedNode;
				this.tvFileResultList1.SelectedNode.BackColor = System.Drawing.Color.Yellow;
				FrmCompareResult.tnTreeView1PreSelectedNode = this.tvFileResultList1.SelectedNode;
			}
		}

		private void FrmCompareResult_Resize(object sender, EventArgs e)
		{
		}

		private string GetParameterValue(string index, string paraName, ElementTypeResultPacker input)
		{
			for (int i = 0; i < input.parameters.Count; i++)
			{
				if (!(index != input.parameters.ElementAt(i).Key))
				{
					for (int j = 0; j < input.parameters.ElementAt(i).Value.Count; j++)
					{
						if (paraName == input.parameters.ElementAt(i).Value[j].Key)
						{
							return input.parameters.ElementAt(i).Value[j].Value;
						}
					}
				}
			}
			return "无";
		}

		private void btnDetailMessage_Click(object sender, EventArgs e)
		{
			DataTable dataTable = new DataTable();
			dataTable.Columns.Add("参数");
			dataTable.Columns.Add("文件1");
			dataTable.Columns.Add("文件2");
			if (FrmCompareResult.tnTreeView1PreSelectedNode == null || FrmCompareResult.tnTreeView2PreSelectedNode == null)
			{
				YJKMessageBox.Show("请选择需要比较的节点");
				return;
			}
			ElementTypeResultPacker elementTypeResultPacker = FrmCompareResult.tnTreeView1PreSelectedNode.Tag as ElementTypeResultPacker;
			ElementTypeResultPacker elementTypeResultPacker2 = FrmCompareResult.tnTreeView2PreSelectedNode.Tag as ElementTypeResultPacker;
			if (elementTypeResultPacker.Name == elementTypeResultPacker2.Name)
			{
				if (elementTypeResultPacker.parameters.Count > elementTypeResultPacker2.parameters.Count)
				{
					for (int i = 0; i < elementTypeResultPacker.parameters.Count; i++)
					{
						string key = elementTypeResultPacker.parameters.ElementAt(i).Key;
						for (int j = 0; j < elementTypeResultPacker.parameters.ElementAt(i).Value.Count; j++)
						{
							DataRow dataRow = dataTable.NewRow();
							dataRow[0] = elementTypeResultPacker.parameters.ElementAt(i).Value[j].Key;
							dataRow[1] = elementTypeResultPacker.parameters.ElementAt(i).Value[j].Value;
							dataRow[2] = this.GetParameterValue(key, elementTypeResultPacker.parameters.ElementAt(i).Value[j].Key, elementTypeResultPacker2);
							if (!dataRow[1].Equals(dataRow[2]))
							{
								dataTable.Rows.Add(dataRow);
							}
						}
					}
				}
				else
				{
					for (int k = 0; k < elementTypeResultPacker2.parameters.Count; k++)
					{
						string key = elementTypeResultPacker2.parameters.ElementAt(k).Key;
						for (int l = 0; l < elementTypeResultPacker2.parameters.ElementAt(k).Value.Count; l++)
						{
							DataRow dataRow = dataTable.NewRow();
							dataRow[0] = elementTypeResultPacker2.parameters.ElementAt(k).Value[l].Key;
							dataRow[1] = this.GetParameterValue(key, elementTypeResultPacker2.parameters.ElementAt(k).Value[l].Key, elementTypeResultPacker);
							dataRow[2] = elementTypeResultPacker2.parameters.ElementAt(k).Value[l].Value;
							if (elementTypeResultPacker2.parameters.ElementAt(k).Value[l].Key == "制造商")
							{
								this.GetParameterValue(key, elementTypeResultPacker2.parameters.ElementAt(k).Value[l].Key, elementTypeResultPacker);
								string value = elementTypeResultPacker2.parameters.ElementAt(k).Value[l].Value;
							}
							if (!dataRow[1].Equals(dataRow[2]))
							{
								dataTable.Rows.Add(dataRow);
							}
						}
					}
				}
				new FrmResultDetail(dataTable, this.m_RevitHandle).Show(this.m_RevitHandle);
				return;
			}
			YJKMessageBox.Show("请选择两个文档均有的节点进行比较");
		}

		private void btnExportReport_Click(object sender, EventArgs e)
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.Filter = "xls|*.xls";
			saveFileDialog.FilterIndex = 1;
			saveFileDialog.InitialDirectory = "C:\\Users\\Administrator\\Desktop\\";
			saveFileDialog.RestoreDirectory = true;
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				try
				{
					this.FlexCellInit(saveFileDialog.FileName);
					YJKMessageBox.Show("导出Excel文件成功");
				}
				catch (Exception)
				{
					YJKMessageBox.Show("导出失败，未指定导出文件所在位置");
				}
			}
		}

		private void FlexCellInit(string pathName)
		{
			int num = 2;
			Grid grid = new Grid();
			grid.Rows = 3;
			grid.Cols = 10;
			grid.AutoRedraw = false;
			grid.Refresh();
			grid.Cell(1, 1).Text = "文件属性";
			grid.Cell(1, 1).BackColor = System.Drawing.Color.Gray;
			grid.Cell(1, 2).Text = "族类别";
			grid.Cell(1, 2).BackColor = System.Drawing.Color.Gray;
			grid.Cell(1, 3).Text = "族名称";
			grid.Cell(1, 3).BackColor = System.Drawing.Color.Gray;
			grid.Cell(1, 4).Text = "文件1";
			grid.Cell(1, 4).BackColor = System.Drawing.Color.Gray;
			grid.Cell(1, 5).Text = "文件2";
			grid.Cell(1, 5).BackColor = System.Drawing.Color.Gray;
			grid.FixedRows = 1;
			for (int i = 0; i < this.tvFileResultList1.Nodes.Count; i++)
			{
				TreeNode treeNode = this.tvFileResultList1.Nodes[i];
				TreeNode treeNode2 = this.tvFileResultList2.Nodes[i];
				DocumentXlsStruct inputXlsStructLeft = new DocumentXlsStruct(treeNode.Name);
				inputXlsStructLeft.Init(treeNode);
				DocumentXlsStruct inputXlsStructRight = new DocumentXlsStruct(treeNode2.Name);
				inputXlsStructRight.Init(treeNode2);
				int rows = grid.Rows;
				this.CreateFlexCellGrid(inputXlsStructLeft, inputXlsStructRight, ref num, ref grid);
				grid.Range(rows - 1, 1, grid.Rows - 2, 1).Merge();
			}
			grid.AutoRedraw = true;
			grid.Refresh();
			grid.ExportToExcel(pathName, false, false);
		}

		private void CreateFlexCellGrid(DocumentXlsStruct inputXlsStructLeft, DocumentXlsStruct inputXlsStructRight, ref int rowNum, ref Grid grid1)
		{
			grid1.Cell(rowNum, 1).Text = inputXlsStructLeft.DocName;
			for (int i = 0; i < inputXlsStructLeft.DocXlsStruct.Count; i++)
			{
				int firstRow = rowNum;
				grid1.Cell(rowNum, 2).Text = inputXlsStructLeft.DocXlsStruct.ElementAt(i).Key;
				for (int j = 0; j < inputXlsStructLeft.DocXlsStruct.ElementAt(i).Value.Count; j++)
				{
					int firstRow2 = rowNum;
					grid1.Cell(rowNum, 3).Text = inputXlsStructLeft.DocXlsStruct.ElementAt(i).Value.ElementAt(j).Key;
					for (int k = 0; k < inputXlsStructLeft.DocXlsStruct.ElementAt(i).Value.ElementAt(j).Value.Count; k++)
					{
						Grid grid2 = grid1;
						int rows = grid2.Rows;
						grid2.Rows = rows + 1;
						string key = inputXlsStructLeft.DocXlsStruct.ElementAt(i).Value.ElementAt(j).Value[k].Key;
						string key2 = inputXlsStructRight.DocXlsStruct.ElementAt(i).Value.ElementAt(j).Value[k].Key;
						System.Drawing.Color value = inputXlsStructLeft.DocXlsStruct.ElementAt(i).Value.ElementAt(j).Value[k].Value;
						System.Drawing.Color value2 = inputXlsStructRight.DocXlsStruct.ElementAt(i).Value.ElementAt(j).Value[k].Value;
						grid1.Cell(rowNum, 4).Text = key;
						grid1.Cell(rowNum, 5).Text = key2;
						grid1.Cell(rowNum, 4).ForeColor = value;
						grid1.Cell(rowNum, 5).ForeColor = value2;
						rowNum++;
					}
					int num = rowNum;
					grid1.Range(firstRow2, 3, num - 1, 3).Merge();
				}
				int num2 = rowNum;
				grid1.Range(firstRow, 2, num2 - 1, 2).Merge();
			}
		}

		private void FrmCompareResult_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
				return;
			}
		}

		private static TreeNode tnTreeView1PreSelectedNode;

		private static TreeNode tnTreeView2PreSelectedNode;

		private DocumentResultPacker DocumentResult1;

		private DocumentResultPacker DocumentResult2;

		private WindowHandle m_RevitHandle;
	}
}
